package com.pignest.hotel.ui.income;

import java.util.Date;
import java.util.List;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Button;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.AdjustAccountHistory;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.TbAccounts;
import com.pignest.hotel.beans.User;
import com.pignest.hotel.definition.LivingState;
import com.pignest.hotel.service.IncomeService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Hotel;
import com.pignest.hotel.system.InComeUtil;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.MoneyUtil;
import com.pignest.hotel.util.UIUtils;

public class EditAccountsWindow extends Window implements EventListener {

	/**
	 * 调账
	 */
	private static final long serialVersionUID = 914291288284665291L;

	private IncomeService incomeService;
	private Living curLiving;
	private Label roomName;
    private Label customName;
    private Label livingTime;
    private Label storage;
    private Label used;
    private Label balance;
    private Label expiredTime;
    private Label grantTime;
    //private Label liveType;
    private Label payed;
    
    private Label accName;
    private Textbox accPrice;
    private Label accPrice1;
    private Label accCounts;
    private Textbox adjustReason;
    private Button okBtn;
    private Listbox accoutsListbox;
    private Listbox adjustHisListbox;
	public EditAccountsWindow()
	{
		incomeService =Services.getIncomeService();
		addEventListener("onCreate", this);
	}
	@Override
	public void onEvent(Event event) throws Exception {
		// TODO Auto-generated method stub
		if ("onCreate".equals(event.getName())) {
			roomName=(Label)getFellow("roomName");
			customName=(Label)getFellow("customName");
			livingTime=(Label)getFellow("livingTime");
			storage=(Label)getFellow("storage");
			used=(Label)getFellow("used");
			balance=(Label)getFellow("balance");
			expiredTime=(Label)getFellow("expiredTime");
			grantTime=(Label)getFellow("grantTime");
			payed =(Label)getFellow("payed");
		//	liveType =(Label)getFellow("liveType");
			accoutsListbox=(Listbox)getFellow("accoutsListbox");
			adjustHisListbox=(Listbox)getFellow("adjustHisListbox");
			accName =(Label)getFellow("accName");
			accPrice=(Textbox)getFellow("accPrice");
			accPrice1=(Label)getFellow("accPrice1");
			accCounts=(Label)getFellow("accCounts");
			adjustReason=(Textbox)getFellow("adjustReason");
			okBtn =(Button)getFellow("okBtn");
			init();
		}
	}
	public void init()
	{
		if(this.curLiving==null)
			return ;
		roomName.setValue(curLiving.getRoom().getName());
		customName.setValue(curLiving.getCustormName());
		livingTime.setValue(DateUtil.toString(curLiving.getLiveTime(), DateUtil.HOTEL_DEFAULT_FORMAT));
		expiredTime.setValue(DateUtil.toString(curLiving.getExpectLeaveDate(),DateUtil.HOTEL_DEFAULT_FORMAT));
		grantTime.setValue(DateUtil.toString(curLiving.getGrantDate(),DateUtil.HOTEL_DEFAULT_FORMAT));
		InComeUtil.countInComeNoAdd(curLiving, storage, used, payed, balance);
		query();
	}
	public void query()
	{
		if(curLiving==null)
			return ;
		accoutsListbox.getItems().clear();
		List<TbAccounts> l=incomeService.queryConsume(curLiving);
		if(l==null){
			return;
		}
		int index=1;
		for(final TbAccounts ac:l)
		{
			 Listitem listitem = new Listitem();
			 listitem.setValue(ac);
			 listitem.setParent(accoutsListbox);
			 listitem.appendChild(new Listcell(String.valueOf(index)));
			 listitem.appendChild(new Listcell(ac.getName()));
			 listitem.appendChild(new Listcell(Utils.toPrice(ac.getPrice())));
			 listitem.appendChild(new Listcell(Utils.toPrice(ac.getPrice2())));
			 listitem.appendChild(new Listcell(String.valueOf(ac.getCounts())));
			 listitem.appendChild(new Listcell(ac.getEndstate()));
			 listitem.appendChild(new Listcell(ac.getRemark()));
			 listitem.addEventListener(Events.ON_CLICK,  new EventListener(){
				   public void onEvent(Event event) throws Exception {
					   //checkButton(l);
					   setSelectAcc(ac);
				   }
			   });
		}
		adjustHisListbox.getItems().clear();
		List<AdjustAccountHistory> aHis =incomeService.queryAdjustAccHis(curLiving.getId(), null, null, null, null, null, null);
	
		for(AdjustAccountHistory h:aHis)
		{
			 Listitem listitem = new Listitem();
			 listitem.setValue(h);
			 listitem.setParent(adjustHisListbox);
			 listitem.appendChild(new Listcell(h.getName()));
			 listitem.appendChild(new Listcell(Utils.toPrice(h.getPrice())));
			 listitem.appendChild(new Listcell(Utils.toPrice(h.getoPrice())));
			 listitem.appendChild(new Listcell(DateUtil.toString(h.getAdjustTime(), DateUtil.HOTEL_DEFAULT_FORMAT)));
			 listitem.appendChild(new Listcell(h.getAdjustUser()));
			 //listitem.appendChild(new Listcell(h.getGrantUser()));	
			 listitem.appendChild(new Listcell(h.getAdjustReason()));
		}
		
	}
	public void adjust()
	{
		TbAccounts selectAc =(TbAccounts)UIUtils.getSelectedValue(accoutsListbox);
		if(selectAc==null)
		{
			UIUtils.alert("请选择一条记录！");
			return ;
		}
		String reason =adjustReason.getValue();
		String aPrice =accPrice.getValue();
		if(!Utils.checkPrice(aPrice)){
			UIUtils.alert("请输入合法值，非负数,小数不能超过2位");
			return ;
		}
		if(aPrice==null)
		{
			UIUtils.alert("调整金额不能为空！");
			accPrice.focus();
			return ;
		}
		if(reason==null||reason.equals(""))
		{
			UIUtils.alert("调整原因不能为空！");
			adjustReason.focus();
			return ;
		}
		User user=Hotel.getCurrentUser();
		int oPrice =selectAc.getPrice();
		StringBuffer msg = new StringBuffer();
		msg.append("您好:").append(user.getName()).append("\n");
		msg.append("调整房间：").append(curLiving.getRoom().getName()).append("\n");
		msg.append("调整所属时间：").append(selectAc.getOpdate()).append("\n");
		msg.append("调整项目:").append(selectAc.getName()).append("\n");
		msg.append("调整前：").append(Utils.toPrice(MoneyUtil.Positive(oPrice))).append("\n");
		msg.append("调整后:").append(aPrice).append("\n");
		msg.append("你确定吗?");
		
		if(UIUtils.confirm(msg.toString())){
			selectAc.setPrice(-Utils.toPrice(aPrice));//转化分
			selectAc.setPrice2(Utils.toPrice(aPrice));
			AdjustAccountHistory ah = new AdjustAccountHistory();
			ah.setAccountsId(selectAc.getId());
			ah.setLivingId(curLiving.getId());
			ah.setRoomName(curLiving.getRoom().getName());
			ah.setoPrice(MoneyUtil.Positive(oPrice));//原价格取正(分)
			ah.setPrice(MoneyUtil.Positive(Utils.toPrice(aPrice)));//现价格取正化分
			ah.setAdjustReason(reason);
			ah.setAdjustTime(new Date());
			ah.setName(selectAc.getName());
			ah.setAdjustUser(user.getName());
			ah.setAdjustWorkNo(user.getWorkNumber());
			ah.setGrantUser(null);
			ah.setGrantWorkNo(null);
			curLiving=incomeService.updateAdjustAccount(selectAc,curLiving, ah);
			init();
			okBtn.setDisabled(true);
		}
	}
	public void setSelectAcc(TbAccounts ac)
	{
		if(ac.getEndstate().equals("是"))
			return ;
		accName.setValue(ac.getName());
		accPrice.setValue(Utils.toPrice(MoneyUtil.Positive(ac.getPrice())));
		accPrice1.setValue(Utils.toPrice(ac.getPrice2()));
		accCounts.setValue(String.valueOf(ac.getCounts()));
		okBtn.setDisabled(false);
	}
	public void onChangePrice()
	{
		TbAccounts selectAc =(TbAccounts)UIUtils.getSelectedValue(accoutsListbox);
		if(selectAc==null)
		{
			UIUtils.alert("请选择一条记录！");
			return ;
		}
		int oPrice =selectAc.getPrice();//cent
		String aPrice =accPrice.getValue();//dolar
		String name =selectAc.getName();
		adjustReason.setValue(name+Utils.toPrice(oPrice)+"调整为"+aPrice);
	}
	public Living getCurLiving() {
		return curLiving;
	}
	public void setCurLiving(Living curLiving) {
		this.curLiving = curLiving;
	}

}
